﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;
using System.Data.Linq;

namespace OpenQuarters.WebQuarters.Core
{
    [Serializable]
    [Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
    public abstract class CMSEntity<TEntityType, TKeyType>
        : OpenQuarters.EntityBase.EntityBase<TEntityType, TKeyType, CMSDataContext>
        where TEntityType : OpenQuarters.EntityBase.EntityBase<TEntityType, TKeyType, CMSDataContext>
    {
        private LoggingMode? loggingMode = null;

        public LoggingMode LoggingMode
        {
            get { return loggingMode.HasValue ? loggingMode.Value : (CMSEnvironment.LoggingMode.HasValue ? CMSEnvironment.LoggingMode.Value : Core.LoggingMode.Standard); }
            set { loggingMode = value; }
        }

        public override DataLoadOptions GetLoadOptions()
        {
            return new DataLoadOptions();
        }

        public override void OnCreated()
        {
            base.OnCreated();
        }

        public override void OnLoaded()
        {
            if (this.LoggingMode == LoggingMode.Debug)
            {
                //EventLog.Add(EventLogPriority.L, typeof(TEntityType).Name + ".Load", this.GetKey(this as TEntityType).ToString(), GeneralUtils.ToJSON(this));
            }
            base.OnLoaded();
        }

        public override void OnSaving()
        {
            if (this.LoggingMode != LoggingMode.Debug)
            {
                //EventLog.Add(EventLogPriority.L, typeof(TEntityType).Name + ".Saving", this.GetKey(this as TEntityType).ToString(), GeneralUtils.ToJSON(this));
            }
            base.OnSaving();
        }

        public override void OnSaved()
        {
            if (this.LoggingMode != LoggingMode.Off)
            {
                EventLog.Add(EventLogPriority.L, typeof(TEntityType).Name + ".Save", this.GetKey(this as TEntityType).ToString(), GeneralUtils.ToJSON(this));
            }
            base.OnSaving();
        }

        public override void OnDeleting()
        {
            if (this.LoggingMode != LoggingMode.Debug)
            {
                //EventLog.Add(EventLogPriority.L, typeof(TEntityType).Name + ".Deleting", this.GetKey(this as TEntityType).ToString(), GeneralUtils.ToJSON(this));
            }
            base.OnSaving();
        }

        public override void OnDeleted()
        {
            if (this.LoggingMode != LoggingMode.Off)
            {
                //EventLog.Add(EventLogPriority.L, typeof(TEntityType).Name + ".Delete", this.GetKey(this as TEntityType).ToString(), GeneralUtils.ToJSON(this));
            }
            base.OnSaving();
        }
    }
}
